﻿using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Base.Cache;

namespace CJAtech.BBS.Process
{
    public class ArticleInfoProcess
    {
        //todo：敏感词
        private string ConnectionStringKey { get; set; }
        private string DataObjectParam { get; set; }
        public BizArticleInfo BizArticleInfo { get; set; }
        public ArticleInfoProcess(string connectionStringKey = "", string dataObjectParam = "")
        {
            this.BizArticleInfo = new BizArticleInfo(connectionStringKey, dataObjectParam);
            this.ConnectionStringKey = connectionStringKey;
            this.DataObjectParam = dataObjectParam;
        }

        public ArticleResultInfo GetArticleResultInfo(int articleId, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null)
        {
            return this.GetArticleResultInfo(articleId, expire).CreateArticleResultInfo(isLoadArticleContent, isLoadArticlePictureCollection, expire, this.ConnectionStringKey, this.DataObjectParam);
        }

        public ArticleResultInfo GetArticleResultInfo(int articleId, TimeSpan? expire = null)
        {
            ArticleResultInfo objArticleResultInfo = null;
            ArticleInfo objArticleInfo = this.GetArticleInfo(articleId, expire);
            if (objArticleInfo == null)
            {
                return objArticleResultInfo;
            }
            objArticleResultInfo = new ArticleResultInfo
            {
                ArticleInfo = objArticleInfo
            };
            return objArticleResultInfo;
        }

        public ArticleInfo GetArticleInfo(int articleId, TimeSpan? expire = null)
        {
            if (expire == null)
            {
                expire = TimeSpan.FromSeconds(5);
            }
            return this.BizArticleInfo.GetRecordCache("", articleId, expire.Value);
        }

        public List<ArticleResultInfo> CreateArticleResultInfoList(List<ArticleInfo> objArticleInfoList, bool isLoadArticleContent, bool isLoadArticlePictureCollection = false, TimeSpan? expire = null)
        {
            return objArticleInfoList.CreateArticleResultInfoList(isLoadArticleContent, isLoadArticlePictureCollection, expire);
        }

        public void IncreaseViewCount(int articleId, int incryBy = 1)
        {
            this.BizArticleInfo.Update("ViewCount=ViewCount+" + incryBy, articleId);
        }
    }
}
